<?
namespace pmvc\ioc\xml;

use PHPUnit_Framework_TestCase;
use Exception;
use pmvc\ioc\FactoryObject;

// define some classes for testing
class ObjectA {
	public $objectName;
	public $whatever;
}

// define some classes for testing
class ObjectB {
	public function __construct($prop1, $prop2) {
		$this->prop1 = $prop1;
		$this->prop2 = $prop2;
	}
	public $prop1;
	public $prop2;
}

class ObjectAFactoryObject
	implements FactoryObject {
	public $objectName;
	public $whatever;
	public function createObject() {
		$ret = new ObjectA();
		$ret->objectName = $this->objectName;
		$ret->whatever = $this->whatever;
		return $ret;
	}
}

class XmlObjectFactoryTest
	extends PHPUnit_Framework_TestCase {

	public function testEverything() {
		
		// create the factory
		$factory = new XmlObjectFactory(Array(__DIR__."/testContext1.xml"));
		$factory2 = new XmlObjectFactory(Array(__DIR__."/testContext2.xml"), $factory);
		
		$object1 = $factory->getObjectById("object1");
		$this->assertTrue(isset($object1));
		$this->assertEquals($object1, $factory->getObjectByName("object1"));
		
		$object2 = $factory->getObjectById("object2");
		$this->assertTrue(isset($object2));
		$this->assertEquals($object1, $object2->whatever);
		
		$object3 = $factory->getObjectById("object3");
		$this->assertTrue(isset($object3));
		$this->assertEquals($object3, $factory->getObjectByName("object3"));
		$this->assertEquals($object1, $object3->prop1);
		$this->assertEquals($object2, $object3->prop2);
		
		$object4 = $factory->getObjectById("object4");
		$this->assertTrue(isset($object4));
		$this->assertEquals($object4->prop1, "one");
		$this->assertEquals($object4->prop2, "two");
		
		$object5 = $factory2->getObjectById("object5");
		$this->assertTrue(isset($object5));
		$this->assertEquals($object5->prop1, "one");
		$this->assertEquals($object5->prop2, "two");
		
		$object6 = $factory2->getObjectById("object6");
		$this->assertTrue(isset($object5));
		$this->assertEquals($object6->objectName, "I am object 6");
		$this->assertEquals($object6->whatever, $object1);
		
		try {
			$factory->getObjectById("object5");
			$this->assertTrue(false);
		} catch(Exception $e) {
			$this->assertTrue(true);
		}

		$_object1 = $factory2->getObjectById("object1");
		$this->assertTrue(isset($_object1));
		$this->assertEquals($object1, $_object1);
		
		$this->assertTrue(count($factory->getObjectIds())==4);
		$this->assertTrue(count($factory2->getObjectIds())==2);
		$this->assertTrue(count($factory->getObjectNames())==2);
		$this->assertTrue(count($factory2->getObjectNames())==0);
		
		$this->assertTrue(count($factory->getObjectsOfType("pmvc\ioc\xml\ObjectA"))==2);
		$this->assertTrue(count($factory->getObjectsOfType("pmvc\ioc\xml\ObjectB"))==2);
		$this->assertTrue(count($factory2->getObjectsOfType("pmvc\ioc\xml\ObjectA"))==1);
		$this->assertTrue(count($factory2->getObjectsOfType("pmvc\ioc\xml\ObjectB"))==1);
		
		$this->assertTrue(count($factory->getObjectIdsOfType("pmvc\ioc\xml\ObjectA"))==2);
		$this->assertTrue(count($factory->getObjectIdsOfType("pmvc\ioc\xml\ObjectB"))==2);
		$this->assertTrue(count($factory2->getObjectIdsOfType("pmvc\ioc\xml\ObjectA"))==1);
		$this->assertTrue(count($factory2->getObjectIdsOfType("pmvc\ioc\xml\ObjectB"))==1);
		
		$this->assertTrue(count($factory->getObjectNamesOfType("pmvc\ioc\xml\ObjectA"))==1);
		$this->assertTrue(count($factory->getObjectNamesOfType("pmvc\ioc\xml\ObjectB"))==1);
		$this->assertTrue(count($factory2->getObjectNamesOfType("pmvc\ioc\xml\ObjectA"))==0);
		$this->assertTrue(count($factory2->getObjectNamesOfType("pmvc\ioc\xml\ObjectB"))==0);
	}
}

?>